#!/usr/bin/python
# multiply is a simple templating mechanism for 'multiplying' code.
# 
# Copyright (C) 2002 Demian L. Neidetcher
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
import string
import sys

class multiply:

	def __init__(this):
		this.keyChar = '@' # look for this inside the template
		this.colWrapper = "'" # text to wrap a value, to allow for spaces
		this.valueStanzaMarker = '@'
		this.directiveMarker = '@!' # special messages to send to multiply

		this.usingTemplateFile = 0

	def xferMultiply(this, linesIn):
		values = this.parseValues(linesIn)
		if (not this.usingTemplateFile):
			this.template = this.parseTemplate(linesIn)
		linesOut = this.multiply(values, this.template)
		return linesOut

	def parseValues(this, linesIn):
		valueList = []
		doingValues = 0
		for line in linesIn:
			if (len(string.strip(line)) == 0):
				if (doingValues): 
					return valueList
			else:
				if (string.split(line)[0] == this.directiveMarker):
					this.parseDirective(line)
				else:
					valueList.append(this.parseValueLine(line))
					doingValues = 1

		return valueList 

	def parseDirective(this, lineIn):
		if (string.find(lineIn, 'templateFile') != -1):
			this.usingTemplateFile = 1
			this.template = this.findTemplate(string.split(lineIn)[-1])

	def findTemplate(this, fileNameIn):
		for possibleDir in this.getPossibleTemplateDirs():
			if (os.access('%s%s' % (possibleDir, fileNameIn), os.R_OK)):
				return open('%s%s' % (possibleDir, fileNameIn), 'r').readlines()
		return '*** NO TEMPLATE FOUND ***'

	def getPossibleTemplateDirs(this):
		possibleDirs = []

		home = os.environ['HOME'] 
		possibleDirs.append('./')
		possibleDirs.append('%s/' % (home))
		possibleDirs.append('%s/.templates/' % (home))
		possibleDirs.append('%s/.vim/' % (home))
		return possibleDirs

	def parseValueLine(this, lineIn):
		valuesOut = []
		insideQuotes = 0
		columnBuffer = ""
		lineIn = string.strip(lineIn)
		for column in string.split(lineIn[1:]):
			if (insideQuotes):
				columnBuffer = '%s %s' % (columnBuffer, column)
				if (column[-1] == this.colWrapper):
					valuesOut.append(columnBuffer[:-1])
					insideQuotes = 0
					break
			else:
				if (string.find(column, this.colWrapper) == -1):
					valuesOut.append(column)
				elif (column[0] == this.colWrapper and column[-1] == this.colWrapper):
					valuesOut.append(column[1:-1])
				else:
					insideQuotes = 1
					columnBuffer = column[1:]
		return valuesOut

	def parseTemplate(this, linesIn):
		endValueStanza = 0
		currLine = 0
		for line in linesIn:
			if (len(string.split(line)) == 0):
				pass
			else:
				if (string.split(line)[0] == this.valueStanzaMarker):
					endValueStanza = currLine 
			currLine = currLine + 1
		return linesIn[endValueStanza+1:]

	def multiply(this, valuesIn, templateIn):
		linesOut = []
		for valueSet in valuesIn:
			linesOut = linesOut + this.applyTemplate(valueSet, templateIn)
		return linesOut 

	def applyTemplate(this, valueSetIn, templateIn):
		linesOut = []
		for line in templateIn:
			valIndex = 0 
			for value in valueSetIn:
				valIndex = valIndex + 1
				key = "%s%s" % (this.keyChar, valIndex)
				line = string.replace(line, key, value)
			linesOut.append(line)
		return linesOut

if __name__=='__main__':
	m = multiply()
	for outLine in m.xferMultiply(sys.stdin.readlines()):
		print outLine,

